package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"video-hub/model/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateNicknameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateNicknameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNicknameLogic {
	return &UpdateNicknameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateNicknameLogic) UpdateNickname(in *videorpc.UpdateNickNameRequest) (*videorpc.UpdateNickNameResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	err := user.FindUserPhone(mysql.DB, in.PhoneNum)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("要修改的用户不存在")
	}
	user = mysql.User{
		Model:    gorm.Model{ID: user.ID},
		NickName: in.NickName,
	}
	err = user.UpdateNickName(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &videorpc.UpdateNickNameResponse{
		UserId: int64(user.ID),
	}, nil
}
